package com.zzh.myapplication.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyLocationStyle;

import net.icarplus.car.R;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.manager.MainSelect;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.ToolBarHelper;
import net.icarplus.car.views.MultiDirectionSlidingDrawer;

/**
 * Created by Administrator on 2016/3/31.
 */
public class BaoMainCarActivity extends BaseActivity implements LocationSource, AMapLocationListener, AMap.OnMarkerClickListener, AMap.OnMapClickListener, View.OnClickListener {
    private MapView mapView;
    private AMap aMap;
    private Toolbar bao_toolbar;
    public AMapLocationClient mLocationClient = null;
    public AMapLocationClientOption mLocationOption = null;
    private UiSettings mUiSettings;
    private Marker currentMarker;
    private LoadingUtil loadingUtil;
    private MarkerOptions markerOption;
    private double getLat = 0.0;
    private double getLon = 0.0;
    public static boolean inNearby = false;
    private MultiDirectionSlidingDrawer mDrawer;

    public LinearLayout ll_day_rent;
    public LinearLayout ll_tang_rent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView = (MapView) findViewById(R.id.amaps);
        mapView.onCreate(savedInstanceState);
    }

    @Override
    public void setContent() {
        setContentView(R.layout.activity_bao_car);
    }

    @Override
    public void initComponent() {
        mapView = (MapView) findViewById(R.id.amaps);
        bao_toolbar = (Toolbar) findViewById(R.id.bao_toolbar);
        MainActivity.mainPage = MainSelect.NEARBY;
        mDrawer = (MultiDirectionSlidingDrawer) findViewById(R.id.drawer);
        ll_day_rent = (LinearLayout) findViewById(R.id.ll_day_rent);
        ll_tang_rent = (LinearLayout) findViewById(R.id.ll_tang_rent);
        mDrawer.open();
    }

    @Override
    public void setListener() {
        ToolBarHelper.setNormalTitleAndBack(this, bao_toolbar, "我要包车");
        this.loadingUtil = new LoadingUtil(this, "定位中...");
        initMap();
        aMap.setOnMarkerClickListener(this);
        aMap.setOnMapClickListener(this);
        ll_day_rent.setOnClickListener(this);
        ll_tang_rent.setOnClickListener(this);
    }

    @Override
    public void onAfterInit() {


    }

    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
            mUiSettings = aMap.getUiSettings();
        }
        this.loadingUtil.startShowLoading();
        mLocationClient = new AMapLocationClient(getApplicationContext());
        mLocationClient.setLocationListener(this);
        mLocationOption = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setNeedAddress(true);
        mLocationOption.setOnceLocation(true);
        mLocationOption.setMockEnable(false);
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory
                .fromResource(R.mipmap.dibiao));// 设置小蓝点的图标
        myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色
        myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色
        myLocationStyle.strokeWidth(0f);// 设置圆形的边框粗细
        aMap.setMyLocationStyle(myLocationStyle);
        aMap.setMyLocationRotateAngle(180);
        aMap.setLocationSource(this);// 设置定位监听
        mUiSettings.setMyLocationButtonEnabled(false); // 是否显示默认的定位按钮
        aMap.setMyLocationEnabled(true);// 是否可触发定位并显示定位层
        mUiSettings.setScaleControlsEnabled(true);// 设置地图默认的比例尺是否显示
        mUiSettings.setZoomControlsEnabled(false);
        mLocationClient.setLocationOption(mLocationOption);
        mLocationClient.startLocation();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        mLocationClient.stopLocation();
        mLocationClient.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            this.loadingUtil.stopShowLoading();
            getLat = amapLocation.getLatitude();
            getLon = amapLocation.getLongitude();
            moveToCurrentLoc(getLat, getLon);
            markerOption = new MarkerOptions();
            markerOption.icon(BitmapDescriptorFactory
                    .fromResource(R.mipmap.zb2));
            markerOption.position(new LatLng(amapLocation.getLatitude(),
                    amapLocation.getLongitude()));
            markerOption.draggable(true);
            aMap.addMarker(markerOption);
        }
    }


    @Override
    public void activate(LocationSource.OnLocationChangedListener onLocationChangedListener) {


    }

    @Override
    public void deactivate() {

    }

    @Override
    public void onMapClick(LatLng latLng) {
        if (currentMarker != null) {
            currentMarker.hideInfoWindow();
        }

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        currentMarker = marker;
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_day_rent:
                start_Activity(new Intent(BaoMainCarActivity.this, BaoCharteredActivity.class));
                break;
            case R.id.ll_tang_rent:
                start_Activity(new Intent(BaoMainCarActivity.this, BaoTangCharteredActivity.class));
                break;
        }
    }

    private void moveToCurrentLoc(double lat, double lon) {
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(
                lat, lon)));
        aMap.moveCamera(CameraUpdateFactory.zoomTo(16));
    }
}
